﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevelopTool.Bll.NoteBlls;
using DevelopTool.Common;
using DevelopTool.Forms.CommonForms;
using DevelopTool.Model;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.Enums;
using Sunny.UI;

namespace DevelopTool.Forms.CommonHelpers
{
    public class NoteHelper
    {
        /// <summary>
        /// 将文本保存到笔记
        /// </summary>
        /// <param name="text"></param>
        public static void SaveToNote(string text, string textType = "Text")
        {
            if (string.IsNullOrEmpty(text)) return;

            NoteBll noteBll = new NoteBll();

            ChoiceMenuForm choiceMenuForm = new ChoiceMenuForm("保存为笔记", "笔记" + DateTime.Now.DateTimeString(false), noteBll.GetList(MenuNodeType.Folder).Cast<ManageMenu>());
            choiceMenuForm.MenuAdd += delegate (object ss, MenuEventArgs ee)
            {
                Model.NoteModels.Note note = new Model.NoteModels.Note()
                {
                    NodeName = ee.ManageMenu.NodeName,
                    NodeType = ee.ManageMenu.NodeType,
                    NodePid = ee.ManageMenu.NodePid,
                    TextType = textType
                };

                if (ee.ManageMenu.NodeType == MenuNodeType.Node)//只保存在节点上
                {
                    note.Text = text;
                }

                ee.ManageMenu = noteBll.Save(note);
                UINotifierHelper.ShowNotifier($"【{note.NodeName}】保存成功", UINotifierType.INFO, UIStyles.CurrentResources.InfoTitle, false, 2000);
            };

            choiceMenuForm.ShowDialog();
        }


        /// <summary>
        /// 将DataGridView选择的单元格内容保存到笔记
        /// </summary>
        /// <param name="dataGridView"></param>
        public static void SaveToNote(DataGridView dataGridView)
        {
            if (dataGridView == null) return;

            List<int> columnIndexList = new List<int>();
            List<int> rowIndexList = new List<int>();

            foreach (DataGridViewCell cell in dataGridView.SelectedCells)
            {
                if (!columnIndexList.Any(it => it == cell.ColumnIndex))
                    columnIndexList.Add(cell.ColumnIndex);

                if (!rowIndexList.Any(it => it == cell.RowIndex))
                    rowIndexList.Add(cell.RowIndex);
            }

            columnIndexList.Reverse();
            rowIndexList.Reverse();

            List<string> rowStrList = new List<string>();

            foreach (int rowIndex in rowIndexList)
            {
                List<string> columnStrList = new List<string>();
                foreach (int columnIndex in columnIndexList)
                {
                    columnStrList.Add(dataGridView.Rows[rowIndex].Cells[columnIndex].Value.ToString());
                }
                rowStrList.Add(string.Join("\t",columnStrList));
            }

            NoteBll noteBll = new NoteBll();

            ChoiceMenuForm choiceMenuForm = new ChoiceMenuForm("保存为笔记", "笔记" + DateTime.Now.DateTimeString(false), noteBll.GetList(MenuNodeType.Folder).Cast<ManageMenu>());
            choiceMenuForm.MenuAdd += delegate (object ss, MenuEventArgs ee)
            {
                Model.NoteModels.Note note = new Model.NoteModels.Note()
                {
                    NodeName = ee.ManageMenu.NodeName,
                    NodeType = ee.ManageMenu.NodeType,
                    NodePid = ee.ManageMenu.NodePid,
                };

                if (ee.ManageMenu.NodeType == MenuNodeType.Node)//只保存在节点上
                {
                    if (dataGridView.SelectedCells.Count > 1)
                    {
                        note.Text = string.Join("\n",rowStrList);
                    }
                    else
                    {
                        if (dataGridView.SelectedCells[0].OwningColumn.Tag is DataGridColumn dataGridColumn)
                        {
                            if (dataGridColumn.ShowFormProperty == null)
                            {
                                note.Text = dataGridView.SelectedCells[0].Value.ToString();
                            }
                            else
                            {
                                note.Text = dataGridView.SelectedCells[0].Value.ToString();
                                note.TextType = dataGridColumn.ShowFormProperty.TextType;
                            }
                        }
                        else
                        {
                            note.Text = string.Join("\n",rowStrList);
                        }
                    }
                }

                ee.ManageMenu = noteBll.Save(note);
                UINotifierHelper.ShowNotifier($"【{note.NodeName}】保存成功", UINotifierType.INFO, UIStyles.CurrentResources.InfoTitle, false, 2000);
            };
            choiceMenuForm.ShowDialog();
        }
    }
}
